<?php
class itemdao extends CI_Model{
	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
	}

	function create($name, $description, $brand,
			$quantity_type, $market_item_type){
		$data = array(
				'item_name'	=> $name,
				'item_description'	=> $description,
				'item_brand_name'	=> $brand,
				'quantity_type_name'	=> $quantity_type,
				'market_item_type_name' => $market_item_type,
		);

		$str = $this->db->insert_string('item', $data);
		$res = $this->db->query($str);
		if (!$res) {
			return 0;
		} else {
			$return_id = $this->db->insert_id();
			array_push($data,
			array('item_id' => $return_id));
			return $data;
		}

	}
	

	function del($item_id) {
	 if (intval($item_id) > 0) {
	 	$this->db->where('item_id', $item_id);
	 	$this->db->delete('item');
	 	return ($this->db->affected_rows() == 1) ? TRUE : FALSE;
	 }
	// return FALSE;
	}
	public function getbyid($id){
		$this->db->select('*');
		$this->db->from('item');
		$this->db->where('item_id',$id);
		$query = $this->db->get();
		$item = $query->row();
		return $item;
	}
	
	public function getitems(){
		$this->db->select('*');
		$this->db->from('item');
		$query = $this->db->get();
		$items = $query->result();
		return $items;
	}
	//function add_branch($neigh
	function get_by_category($market_id, $market_item_category_name){
		if (intval($market_id) > 0 ) {
			$this->db->select('item.*,market_item_type.market_item_category_name');
			$this->db->from('item');
			$this->db->join('market_item_type', 'item.market_item_type_name
					 = market_item_type.market_item_type_name');
			$this->db->where('market_item_type.market_item_category_name',
					 $market_item_category_name);
			$this->db->join('market_item', 'item.item_id
					 = market_item.item_id');
			$this->db->where('market_item.market_id',
					$market_id);
			$query = $this->db->get();
			$result = $query->result();
			
			return $result;
		}
		return null;
	}
	function get_categories_in_market($market_id){
		if (intval($market_id) > 0 ) {
			$this->db->select('market_item_type.market_item_category_name');
				$this->db->group_by('market_item_category_name');
			$this->db->from('item');
			$this->db->join('market_item_type', 'item.market_item_type_name
					 = market_item_type.market_item_type_name');

			$this->db->join('market_item', 'item.item_id
					 = market_item.item_id');
			$this->db->where('market_item.market_id',
					$market_id);
			$query = $this->db->get();
			$result = $query->result();
				
			return $result;
		}
		return null;
	}
	
	function get_item_info($market_item_id){
		if (intval($market_item_id) > 0 ) {
			$this->db->select('item.*');
			$this->db->from('market_item');
			$this->db->join('item', 'market_item.item_id
					 = item.item_id');
			$this->db->where('item.item_id',
					$market_item_id);
			$query = $this->db->get();
			$result = $query->result();
	
			return $result;
		}
		return null;
	}
	
}
?>